import QtQuick 2.0
import QtQuick.Controls 2.12
import QtQuick.Layouts 1.12
import "qrc:/qmlComponents/CustomizedUIElement"
import "qrc:/qmlComponents/ConfigManager"
import ConfigElementType 1.0
import "qrc:/qmlComponents/CustomizedUIElement"
import QtQuick.Dialogs 1.2
import FileContentItem 1.0

Item {
    property var maxListHeight: 700

    Component{
        id: aaCoreConfigger
        RowLayout {
            ColumnLayout {

                GroupBox {
                    title: qsTr("AA")
                    RowLayout {
                        Label {
                            text: qsTr("AA Selected Layer:")
                        }
                        ComboBox {
                            Layout.preferredWidth: 150
                            model: [ "1", "2", "3" ]
                            currentIndex: aaCoreConfig.AASelectedLayer
                            onCurrentIndexChanged: {
                                aaCoreConfig.setAASelectedLayer(currentIndex)
                            }
                        }
                        Label {
                            text: qsTr("Fit Order")
                        }
                        TextField {
                            text: aaCoreConfig.FitOrder
                            horizontalAlignment: TextInput.AlignHCenter
                            validator: IntValidator {
                                bottom: 4
                                top: 6
                            }
                            onEditingFinished: {
                                aaCoreConfig.setFitOrder(text)
                            }
                        }

                        Label {
                            text: qsTr("AA Scan MTF Nyquist Frequency:")
                        }
                        ComboBox {
                            Layout.preferredWidth: 150
                            model: [ "1/8", "1/4" ]
                            currentIndex: aaCoreConfig.SampleFrequency
                            onCurrentIndexChanged: {
                                aaCoreConfig.setSampleFrequency(currentIndex)
                            }
                        }
                    }
                }

                RowLayout {
                    Label {
                        text: qsTr("Oversampling:")
                    }
                    ComboBox {
                        Layout.preferredWidth: 150
                        model: [ "1", "2" ]
                        currentIndex: aaCoreConfig.Oversampling
                        onCurrentIndexChanged: {
                            aaCoreConfig.setOversampling(currentIndex)
                        }
                    }
                    Label {
                        text: qsTr("EFL")
                    }
                    TextField {
                        text: aaCoreConfig.EFL
                        horizontalAlignment: TextInput.AlignHCenter
                        onEditingFinished: {
                            aaCoreConfig.setEFL(text)
                        }
                    }
                }

                RowLayout {
                    Button {
                        text: qsTr("Load AA Debug Image")
                        onClicked: {
                            loadAADebugImageDialog.open()
                        }
                    }
                    TextField {
                        selectByMouse: true
                        implicitWidth: 280
                        text: aaCoreConfig.aaDebugImageFilename
                        onTextEdited: {
                            aaCoreConfig.setAADebugImageFilename(text)
                        }
                    }
                }

                GroupBox {
                    ConfigRow{
                        configModel: aaCoreConfig
                        configName: "FontSizeDisplay"
                        Component.onCompleted: {
                            init(ConfigElementType.Int)
                        }
                    }
                }

                GroupBox {
                    title : qsTr("Pattern Analysis")

                    RowLayout {
                        ConfigRow{
                            configModel: aaCoreConfig
                            configName: "MaxIntensity"
                            Component.onCompleted: {
                                init(ConfigElementType.Int)
                            }
                        }

                        ConfigRow{
                            configModel: aaCoreConfig
                            configName: "MinArea"
                            Component.onCompleted: {
                                init(ConfigElementType.Int)
                            }
                        }
                        ConfigRow{
                            configModel: aaCoreConfig
                            configName: "MaxArea"
                            Component.onCompleted: {
                                init(ConfigElementType.Int)
                            }
                        }
                        Button {
                            text: qsTr("Run AA Debug Image")
                            onClicked: {
                                tem.runSingleCmd(aaCore, "performAADebugImage")
                            }
                        }
                    }
                }

                GroupBox {
                    title: qsTr("OC Analysis")
                    RowLayout {
                        ConfigRow{
                            configModel: aaCoreConfig
                            configName: "SensorXRatio"
                            Component.onCompleted: {
                                init(ConfigElementType.Double)
                            }
                        }
                        ConfigRow{
                            configModel: aaCoreConfig
                            configName: "SensorYRatio"
                            Component.onCompleted: {
                                init(ConfigElementType.Double)
                            }
                        }
                        Button {
                            text: qsTr("OC Calibration")
                            onClicked:  {
                                tem.runSingleCmd(aaCore, "performOCCalibration")
                            }
                        }
                    }
                }

                Button {
                    text: qsTr("Run flowchart")
                    onClicked: {
                        var command = "document.getElementById('get_data').click()";
                        aaFlowcharWebview.runJavaScript(command, function(result) {
                            command = "document.getElementById('flowchart_data').value";
                            aaFlowcharWebview.runJavaScript(command, function(result) {
                                aaCore.loadFlowchart(result)
                                aaCore.runFlowchart()
                            })
                        })
                    }
                }
                Button {
                    text: qsTr("AA Test")
                    onClicked: {
                        tem.runSingleCmd(aaHead, "testCmd")
                    }
                }
            }
            ColumnLayout {
                Frame {
                    contentWidth: 600
                    contentHeight: 600
                    Image {
                        width: 600
                        height: 600
                        id: aaDebugImage
                        source: "qrc:/commonicons/initial.png"
                        fillMode: Image.PreserveAspectFit
                        cache: false
                    }
                    Connections {
                        target: aaImage
                        onDisplayAADebugImage: {
                            aaDebugImage.source = ""
                            aaDebugImage.source = "image://aaDebugImage"
                        }
                    }
                }
            }
        }
    }

    FileDialog {
        id: loadAADebugImageDialog
        title: qsTr("选择加载文件")
        selectExisting: true
        selectFolder: false
        selectMultiple: false

        nameFilters: ["bmp文件 (*.bmp) | jpg文件 (*.jpg)"]
        onAccepted: {
            aaCoreConfig.setAADebugImageFilename(loadAADebugImageDialog.fileUrl)
        }
    }

    MyTabView{
        id: viewer
        anchors.fill: parent
    }

    Component.onCompleted: {
        viewer.addTab("AA Core", aaCoreConfigger)
    }
}
